﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyDoanVien.General;
using QuanLyDoanVien.Controller;

namespace QuanLyDoanVien
{
    public partial class frmMain : Office2007RibbonForm
    {
        public frmMain()
        {
            InitializeComponent();
           // frmFlash m_FrmFlash = new frmFlash();
            //m_FrmFlash.Show();
            //System.Threading.Thread.Sleep(3000);
           // m_FrmFlash.Close();
            DataService.OpenConnection();
        }
        NhanVienCtrl m_NhanVienCtrl = new NhanVienCtrl();
        PhanQuyenCtrl m_PhanQuyenCtrl = new PhanQuyenCtrl();
        frmDangNhap m_FrmDangNhap = null;
       //frmDoiMatKhau temp = new frmDoiMatKhau();
   

        private void frmMain_Load(object sender, EventArgs e)
        {
            Default();

            DangNhap();

            // Load Quick Access Toolbar layout if one is saved from last session...
            Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\DevComponents\Ribbon");
            if (key != null)
            {
                try
                {
                    string layout = key.GetValue("RibbonPadCSLayout", "").ToString();
                    if (layout != "" && layout != null)
                        ribbonControl.QatLayout = layout;
                }
                finally
                {
                    key.Close();
                }
            }

            // Pulse the Application Button
            buttonFile.Pulse(11);
        }

        private void frmMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // Save Quick Access Toolbar layout if it has changed...
            if (ribbonControl.QatLayoutChanged)
            {
                Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\DevComponents\Ribbon");
                try
                {
                    key.SetValue("RibbonPadCSLayout", ribbonControl.QatLayout);
                }
                finally
                {
                    key.Close();
                }
            }
        }
        #region Đăng Nhập
        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            m_FrmDangNhap.txtUserName.Text = "";
            m_FrmDangNhap.txtPass.Text = "";
            m_FrmDangNhap.lbErrorPass.Text = "";
            m_FrmDangNhap.lbErrorUserName.Text = "";

            DangNhap();
        }

        private void btnDangXuat_Click(object sender, EventArgs e)
        {
            Default();
            lblTenNguoiDung.Text = "Bạn cần đăng nhập để sử dụng phần mềm";
        }

        public void DangNhap()
        {
            Cont:
            if (m_FrmDangNhap == null || m_FrmDangNhap.IsDisposed)
                m_FrmDangNhap = new frmDangNhap();

            if (m_FrmDangNhap.ShowDialog() == DialogResult.OK)
            {
                if (m_FrmDangNhap.txtUserName.Text == "")
                {
                    m_FrmDangNhap.lbErrorUserName.Text = "Bạn chưa nhập tên đăng nhập";
                    m_FrmDangNhap.lbErrorPass.Text = "";
                    goto Cont;
                }

                if (m_FrmDangNhap.txtPass.Text == "")
                {
                    m_FrmDangNhap.lbErrorPass.Text = "Bạn chưa nhập mật khẩu";
                    m_FrmDangNhap.lbErrorUserName.Text = "";
                    goto Cont;
                }
                int kq = 2;
                lblTenNguoiDung.Text = "Xin chào: Admin Luong";
                //int kq = m_PhanQuyenCtrl.DangNhap(m_FrmDangNhap.txtUserName.Text, m_FrmDangNhap.txtPass.Text);
                //lblTenNguoiDung.Text = "Xin chào: " + m_PhanQuyenCtrl.LayTenNguoiDung(m_FrmDangNhap.txtUserName.Text);
                //temp.userName = m_FrmDangNhap.txtUserName.Text.ToString();
                switch (kq)
                {
                    case 0:
                        m_FrmDangNhap.lbErrorPass.Text = "";
                        m_FrmDangNhap.lbErrorUserName.Text = "Người dùng không tồn tại";
                        goto Cont;
                    case 1:
                        m_FrmDangNhap.lbErrorUserName.Text = "";
                        m_FrmDangNhap.lbErrorPass.Text = "Mật khẩu không hợp lệ";
                        goto Cont;
                    case 2:
                        if (m_PhanQuyenCtrl.PhanQuyen == "MQ001")
                            Admin();
                        else
                            Admin();
                            //Users();
                        break;
                }
            }

        }

        public void Default()
        {
            //btnAbout.Enabled = true;
            //btnCalculator.Enabled = true;
            btnClose.Enabled = true;
            //btnHelp.Enabled = true;

            btnDangNhap.Enabled = true;
            btnBackUp.Enabled = false;
            btnRestor.Enabled = false;
            btnChangePass.Enabled = false;
            btnDangXuat.Enabled = false;

            //btnThemMatHang.Enabled = false;
            //btnXuatHang.Enabled = false;
            //btnDatHang.Enabled = false;

            //btnKhachHang.Enabled = false;

            //btnQuayGiaoDich.Enabled = false;
            //btnNhanVien.Enabled = false;
            //btnNhomNguoiDung.Enabled = false;
            //btnQuyen.Enabled = false;
            //btnPhanNhom.Enabled = false;
            //btnPhanQuyen.Enabled = false;
            btnChangePass.Enabled = false;
        }

        public void Admin()
        {
            //btnHelp.Enabled = true;
            //btnAbout.Enabled = true;
            //btnCalculator.Enabled = true;
            btnClose.Enabled = true;
            btnDangNhap.Enabled = true;
            btnBackUp.Enabled = true;
            btnRestor.Enabled = true;
            btnChangePass.Enabled = true;
            btnDangXuat.Enabled = true;

            //btnThemMatHang.Enabled = true;
            //btnXuatHang.Enabled = true;
            //btnDatHang.Enabled = true;

            //btnKhachHang.Enabled = true;

            //btnQuayGiaoDich.Enabled = true;
            //btnNhanVien.Enabled = true;

            //btnNhomNguoiDung.Enabled = true;
            //btnQuyen.Enabled = true;
            //btnPhanNhom.Enabled = true;
            //btnPhanQuyen.Enabled = true;
            btnChangePass.Enabled = true;
        }

        public void Users()
        {
            //btnAbout.Enabled = true;
            //btnCalculator.Enabled = true;
            btnClose.Enabled = true;
            //btnHelp.Enabled = true;

            btnDangNhap.Enabled = true;
            btnBackUp.Enabled = false;
            btnRestor.Enabled = false;
            btnChangePass.Enabled = true;
            btnDangXuat.Enabled = true;

            //btnThemMatHang.Enabled = true;
            //btnXuatHang.Enabled = true;
            //btnDatHang.Enabled = true;

            //btnKhachHang.Enabled = true;

            //btnQuayGiaoDich.Enabled = true;
            //btnNhanVien.Enabled = true;

            //btnNhomNguoiDung.Enabled = false;
            //btnQuyen.Enabled = false;
            //btnPhanNhom.Enabled = false;
            //btnPhanQuyen.Enabled = false;
            btnChangePass.Enabled = true;
        }
        #endregion
      
       
        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBoxEx.Show("Bạn có muốn thoát khỏi phần mềm quản lý đoàn viên không?", "THOÁT KHỎI CHƯƠNG TRÌNH", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                e.Cancel = true;
            }

        }

        private void cbx_HS_TimTheo_SelectedIndexChanged(object sender, EventArgs e) {
            if (cbx_HS_TimTheo.SelectedItem.ToString().Equals(comboItem4.Text)) {
                lbl_HS_SaiPham.Visible = true;
                txt_HS_SaiPham.Visible = true;
            } else {
                lbl_HS_SaiPham.Visible = false;
                txt_HS_SaiPham.Visible = false;
            }
        }
    }
}
